package homework.work3.dao;

import homework.work3.model.Product;
import homework.work3.model.ProductData;

public class ProductDaoImpl implements ProductDao {
    private static Product[] products = ProductData.PRODUCTS;

    @Override
    public Product[] queryAllProducts() {
        return products;
    }

    @Override
    public boolean addProducts(Product product) {
        for (int i = 0; i < products.length; i++) {
            if (products[i] == null) {
                products[i] = product;
                return true;
            }
        }
        return false;
    }

    @Override
    public Product getProductsID(int i) {
        Product product = null;
        for (Product p : products) {
            if (p != null) {
                if (p.getId() == i) {
                    product = p;
                }
            }
        }
        return product;
    }

    @Override
    public Product getProductsName(String name) {
        Product product=null;
        for (Product p : products) {
            if(p!=null){
                if(p.getName().equals(name)){
                    product=p;
                }
            }
        }
        return product;
    }

    @Override
    public Product updateProduct(Product p) {
        Product product=null;
        for(int i=0;i<products.length;i++){
            if(products[i].getId()==p.getId()){
                products[i].setName(p.getName());
                product=products[i];
                break;
            }
        }
        return product;
    }
}
